package com.lyl.syt.api;

import com.lyl.syt.common.result.Result;
import com.lyl.syt.common.utils.AuthContextHolder;
import com.lyl.syt.model.user.UserInfo;
import com.lyl.syt.service.UserInfoService;
import com.lyl.syt.vo.user.LoginVo;
import com.lyl.syt.vo.user.UserAuthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
public class UserInfoController {
    private final UserInfoService userInfoService;

    @ApiOperation("登录接口")
    @PostMapping("/login")
    public Result<Map<String,Object>> login(@RequestBody LoginVo loginVo){
        Map<String,Object> map = userInfoService.loginUser(loginVo);
        return Result.ok(map);
    }

    //用户认证接口
    @ApiOperation("用户认证")
    @PostMapping("/auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo authVo, HttpServletRequest request){
        // 用户认证，参数1：用户id;  参数2：认证数据vo对象
        userInfoService.userAuth(AuthContextHolder.getUserId(request),authVo);
        return Result.ok();
    }

    //获取用户信息
    @ApiOperation("获取用户信息")
    @GetMapping("/auth/getUserInfo")
    public Result<UserInfo> getUserInfo(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        return Result.ok(userInfoService.getById(userId));
    }


}
